import storesApi from "../api/stores";
import goodsApi from '../api/shopGoods'

export default {
  namespaced: true,
  state: {
    curPage: 1,
    eachPage: 5,
    total: 0,
    maxPage: 0,
    rows: [],
    name: "",
    address: "",
    hasDistribution: false,
    StShop: [],
  },
  mutations: {
    //新增店铺
    async addStores(state, data) {
      const { name, address, tel, business, time, status, hasDistribution } =
        data;
      const result = await storesApi.post(
        name,
        address,
        tel,
        business,
        time,
        status,
        hasDistribution
      );
      return result;
    },
    //获取店铺列表
    get(state, stores) {
      stores.rows.forEach((value) => {
        return (
          (value.business =
            value.business === true ? "正在营业中" : "暂停营业中"),
          (value.hasDistribution = value.hasDistribution === true ? "是" : "否")
        );
      });
      Object.assign(state, stores);
    },
    //店铺列表上下页变动
    setCurPage(state, curPage) {
      state.curPage = curPage;
    },
    //店铺列表每页显示条数变动
    setEachPage(state, eachPage) {
      state.eachPage = eachPage;
    },
    //修改店铺信息
    async editStores(state, data) {
      const {
        name,
        address,
        tel,
        business,
        time,
        status,
        hasDistribution,
        _id,
      } = data;
      const result = await storesApi.editStores(
        name,
        address,
        tel,
        business,
        time,
        status,
        hasDistribution,
        _id
      );
      return result;
    },
    //删除店铺
    async deleteStores(state, data) {
      console.log("store", data);
      const { _id } = data;
      const result = await storesApi.deleteStores(_id);
      return result;
    },
    //查询店铺
    async searchStores(state, data) {
      const { curPage, eachPage } = this.state.stores;
      data.curpage = curPage;
      data.eachpage = eachPage;
      const { name, address, curpage, eachpage } = data;
      const result = await storesApi.searchStores(
        name,
        address,
        curpage,
        eachpage
      );
      result.rows.forEach((value) => {
        return (
          (value.business =
            value.business === true ? "正在营业中" : "暂停营业中"),
          (value.hasDistribution = value.hasDistribution === true ? "是" : "否")
        );
      });
      Object.assign(state, result);
    },
    //新增店铺商品
    async addStShop(state, data) {
      const { goodsInfo, storesId } = data
      const result = await storesApi.postStShop(goodsInfo, storesId)
      let a = { ...state, StShop: result.storeGoods }
      Object.assign(state,a)
    },
    //获取所有商品列表
    updataGoods(state, goods) {
      Object.assign(state, goods)
    },
    //获取店铺商品列表
    getStShops(state, data) {
      //因原有数据不能修改，没有把data直接加在StShop上，所以在新数据上进行修改
      let a = { ...state, StShop: data }
      Object.assign(state, a)
    },
    //查找商品
    async serchDoods(state, serName) {
      const data = await goodsApi.serchGoods(serName)
      state.rows = data
    },
    //删除店铺商品
    async deleteStShop(state, data) {
      const { index, goodsInfo, storesId } = data
      const result = await storesApi.deleteStShop(index, goodsInfo, storesId);
      let a = { ...state, StShop: result.storeGoods }
      Object.assign(state,a)
    },
  },
  actions: {
    //获取店铺列表
    async getStores({ state, commit }) {
      const { curPage, eachPage } = state;
      const data = await storesApi.get(curPage, eachPage);
      commit("get", data);
    },
    //获取所有商品列表
    async getGoods({ state, commit }) {
      const { curPage, eachPage } = state
      const data = await goodsApi.get({ curPage, eachPage })
      commit("updataGoods", data)
    },
    //获取店铺商品列表
    async getStShop({ state, commit }, storesId) {
      const { curPage, eachPage } = state;
      const data = await storesApi.getStShop(curPage, eachPage, storesId);
      commit("getStShops", data);
    },
  },
  modules: {},
};
